package com.zx.disneyholidays.utils;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

import com.zx.disneyholidays.GenericFileProviders;
import com.zx.disneyholidays.MyApplication;

import java.io.File;
import java.io.IOException;

/**
 * Created by xiewei on 2017/11/10.
 */

public class FileUtils {
    public static final String IMAGE_CACHE_PATH = MyApplication.applicationContext.getExternalCacheDir() + "/image";

    public static final File CACHE_DIR_FILE = isExternalStorageAvailable() ? MyApplication.applicationContext.getExternalCacheDir() : MyApplication.applicationContext.getCacheDir();
    public static final String CAHCE_DIR_PATH = CACHE_DIR_FILE.getAbsolutePath();

    public static long getAvailableMemorySize(String path) {
        if (!TextUtils.isEmpty(path)) {
            try {
                StatFs stat = new StatFs(path);
                long blockSize = getBlockSize(stat);
                long availableBlocks = getAvailableBlocks(stat);
                return availableBlocks * blockSize;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public static long getSDCardAvailableSize() {
        if (isExternalStorageAvailable()) {
            return getAvailableMemorySize(Environment.getExternalStorageDirectory().toString());
        } else {
            return 0;
        }
    }

    public static long getSDCardTotalSize() {
        if (isExternalStorageAvailable()) {
            try {
                StatFs stat = new StatFs(Environment.getExternalStorageDirectory().toString());
                return getTotalBlockSize(stat);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        } else {
            return 0;
        }
    }

    private static long getBlockSize(StatFs stat) {
        int sdk = Build.VERSION.SDK_INT;
        if (sdk < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            return stat.getBlockSize();
        } else {
            return stat.getBlockSizeLong();
        }
    }


    private static long getAvailableBlocks(StatFs stat) {
        int sdk = Build.VERSION.SDK_INT;
        if (sdk < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            return stat.getAvailableBlocks();
        } else {
            return stat.getAvailableBlocksLong();
        }
    }

    public static long getTotalBlockSize(StatFs stat) {
        int sdk = Build.VERSION.SDK_INT;
        if (sdk < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            return stat.getBlockCountLong() * getBlockSize(stat);
        } else {
            return stat.getTotalBytes();
        }
    }

    public static boolean isExternalStorageAvailable() {

        return Environment.getExternalStorageState().equals(

                Environment.MEDIA_MOUNTED);

    }

    public static File getTempFile(String tempFileName) {
        File head = null;
        if (TextUtils.isEmpty(tempFileName)) {
            return null;
        }
        File f = CACHE_DIR_FILE;
        if (f == null) {
            return null;
        }
        if (!f.exists()) {
            f.mkdirs();
        } else {
            if (f.isFile()) {
                f.deleteOnExit();
                f.mkdirs();
            }
        }
        head = new File(f, tempFileName);
        return head;
    }


    /**
     * 创建一个缓存目录下的临时filename文件
     *
     * @param fileName
     * @return
     */
    public static File createTempFile(String fileName) {
        File tempFile = getTempFile(fileName);
        if (tempFile.exists()) {
            tempFile.deleteOnExit();
        }
        try {
            tempFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tempFile;
    }

    /**
     * 得到 @tempFileName 对应的保护性文件Uri
     *
     * @param tempFileName
     * @return
     */
    public static Uri createTempFileUri(String tempFileName) {
        File tempFile = createTempFile(tempFileName);
        Uri uri = null;
        if (tempFile != null) {
            uri = getUriFromFile(MyApplication.applicationContext, tempFile);
        }
        return uri;
    }

//    /**
//     * 得到 @imageName 对应的文件
//     * @param imageName
//     * @return
//     */
//    public static File getImageFile(String imageName) {
//        String filePath = null;
//        if (MyApplication.applicationContext.getExternalCacheDir() != null) {
//            filePath = MyApplication.applicationContext.getExternalCacheDir().getAbsolutePath();
//        }
//        if (!TextUtils.isEmpty(filePath)) {
//            String filepath = filePath + "/" + imageName;
//            return new File(filepath);
//        } else {
//            return null;
//        }
//    }

    /**
     * 得到 @file 文件的保护性Uri
     *
     * @param context
     * @param file
     * @return
     */
    public static Uri getUriFromFile(Context context, File file) {
        Uri fileUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
            fileUri = GenericFileProviders.getUriForFile
                    (context, "com.zx.disneyholidays.GenericFileProviders", file);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;

    }
}
